package com.bawei.park2302.visit

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.lib_base.utils.utils.entity.VisitBeanItem
import com.bawei.lib_base.utils.utils.mvi.BaseActivity
import com.bawei.lib_base.utils.utils.mvi.UiState
import com.bawei.park2302.R
import com.bawei.park2302.databinding.ActivityVisitAddBinding
import com.blankj.utilcode.util.ToastUtils
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@Route(path = "/app/VisitAddActivity")
@AndroidEntryPoint
class VisitAddActivity : BaseActivity<ActivityVisitAddBinding,VisitIntent,VisitViewModel>() {
    override val binding: ActivityVisitAddBinding by lazy {
        ActivityVisitAddBinding.inflate(layoutInflater)
    }
    override val vm: VisitViewModel by viewModels<VisitViewModel>()

    override fun initView() {
        binding.ivAdd.setOnClickListener {
            val appointmentTime = binding.visitAddAppointmentTime.text.toString() + " " + binding.visitAddStartTime.text.toString()
            val employeeId = binding.visitAddEmployeeId.text.toString().toInt()
            val purpose = binding.visitAddPurpose.text.toString()
            val plateNumber = binding.visitAddPlateNumber.text.toString()
            val phoneNumber = binding.visitAddPhoneNumber.text.toString()
            val visitorName = binding.visitAddVisitorName.text.toString()
            val idNumber = binding.visitAddIdNumber.text.toString()
            val requestParking = binding.visitAddSwitch.isChecked

            val visitBeanItem = VisitBeanItem(
                appointmentTime,
                false,
                arrayListOf(""),
                employeeId,
                1,
                idNumber,
                phoneNumber,
                plateNumber,
                purpose,
                requestParking,
                "待审核",
                visitorName
            )

            vm.sendIntent(VisitIntent.visitAdd(visitBeanItem))
        }

        binding.visitAddSwitch.setOnCheckedChangeListener { compoundButton, checkId ->
            binding.visitAddPlateNumber.isEnabled = checkId
        }

        binding.visitAddIvAppointmentTime.setOnClickListener {
            showDataDialog(binding.visitAddAppointmentTime)
        }

        binding.visitAddIvStartTime.setOnClickListener {
            showTimeDialog(binding.visitAddStartTime)
        }

        binding.visitAddIvEndTime.setOnClickListener {
            showTimeDialog(binding.visitAddEndTime)
        }

    }

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        ToastUtils.showLong("添加成功")
                        finish()
                    }
                    else -> {}
                }
            }
        }
    }
}